Skip to content

Separate MCP clients by resolved request headers - #1749

Open
VIOLETee0 wants to merge 1 commit into
CodebuffAI:mainfrom
VIOLETee0:fix/mcp-client-header-identity
Open

VIOLETee0 wants to merge 1 commit into
CodebuffAI:mainfrom
VIOLETee0:fix/mcp-client-header-identity

Conversation

@VIOLETee0

Copy link
Copy Markdown

Problem / Why

Remote MCP clients are cached by transport, endpoint and query parameters, but request headers are omitted. Connecting to the same endpoint with a different authorization header therefore reuses the first client's transport and cached tool catalog. A local MCP server reproduces the second identity receiving the first identity's tools.

This revisits the stale-header case from closed, unmerged #890, with a regression test and a narrower scope.

Changes

  • Include a SHA-256 digest of the environment-resolved headers in HTTP and SSE client cache identities. The returned ID does not contain plaintext header values.
  • Add one real loopback HTTP MCP regression covering distinct authorization identities and reuse of an unchanged identity.

Verification

Validated locally on Windows with Bun 1.3.14, using isolated test environment settings and fictitious authorization values. The branch is based on main c5e985410d548fbf92803fc4e9d64a9142c926a8; the affected tests, probes, static checks and builds were rerun after updating the base.

  • bun test common/src/mcp/__tests__/client-headers.test.ts: failed before the fix (expected who_beta, received who_alpha), passed after it.
  • bun test common/src/mcp/__tests__/: 47 passed, 0 failed.
  • TypeScript checks for common and sdk: passed.
  • SDK build, Windows Freebuff binary build, binary version and official binary smoke test: passed.
  • A separate real HTTP/SSE probe verified environment-header rotation creates distinct clients and restoring the original value reuses its client.
  • ESLint on both changed files: 0 errors, 5 warnings. The production file has the same three warnings as main; the new test has two resolver-environment warnings. The new test passes Prettier; the production file retains its two pre-existing formatting differences.

Notes / Scope

This changes remote cache identity only. It does not add client disposal, connection eviction, retry or replay behavior, or header-name/order canonicalization. No dependency changes. A fresh frozen-lockfile install, the full monorepo suite, Linux/macOS execution and hosted CI have not been run.

@codebuff-team

Copy link
Copy Markdown
Contributor

Good catch: hashConfig previously ignored headers entirely, so two MCP configs differing only by Authorization would collide on the same cached client and reuse the wrong tool catalog. The loopback HTTP regression test in client-headers.test.ts is a legitimate, specific reproduction (not just a mock), and hashing the resolved headers instead of storing plaintext is the right call for the cache key.

Two concerns before this is portable as-is:

  1. Unhandled headers: undefined case. In client.ts, both the HTTP and SSE branches now do createHash('sha256').update(JSON.stringify(substituteEnvInRecord(config.headers))).digest('hex') unconditionally. If config.headers is undefined (the common case for configs with no auth), and substituteEnvInRecord doesn't special-case undefined, JSON.stringify may return undefined, and createHash().update(undefined) throws a TypeError. The new test only covers the headers-present case — there's no test confirming existing configs without headers still work after this change. Given this touches every remote MCP connection, that's the edge case most likely to break users in practice.

  2. JSON.stringify is key-order-sensitive. Two functionally identical header objects with keys in different insertion order will hash differently, causing unnecessary cache misses/duplicate clients. Not a correctness bug per se (you call this out explicitly in the Notes section), but worth at least a sorted-key serialization since it's a one-line fix and avoids subtle churn.

Scope and size are fine — small, focused, in common/, with a real test. Please add a case (or confirm/fix) for the no-headers path before this is portable as a straightforward fix rather than a semi-regression.

@codebuff-team codebuff-team added bot:triaged Classified by the community triage bot pr:needs-work Right idea, not mergeable as written labels Nov 28, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:triaged Classified by the community triage bot pr:needs-work Right idea, not mergeable as written

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants